﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DigDes.DSchool.SUPS.DataAccess.Database;

namespace DigDes.DSchool.SUPS.Web.UI
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        private int PageSize = 10;
        //private int CurrentCargoPage;
        //private int LastCargoPage = 1;

        public WebForm3() : base()
        {            
        }
        


       
        protected void Page_Load(object sender, EventArgs e)
        {            
        }

        public void OnAddButton(Object sender, EventArgs e)
        {
            CargoDetailsView.Visible = true;
            CargoDetailsView.ChangeMode(DetailsViewMode.Insert);
        }

        public void OnDeleteButton(Object sender, EventArgs e)
        {           
            CargoDetailsDataSource.Delete();                       
            GridViewForCargo.DataBind();

            CargoDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            CargoDetailsView.Visible = false;
            EditButtonID.Enabled = false;
            DeleteButtonID.Enabled = false;
        }

        public void OnEditButton(Object sender, EventArgs e)
        {
            CargoDetailsView.ChangeMode(DetailsViewMode.Edit);
        }
        
        public void GridView_OnSelectedIndexChanged(Object sender, EventArgs e)   
        {
            CargoDetailsView.Visible = true;
            CargoDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            
            EditButtonID.Enabled = true;
            DeleteButtonID.Enabled = true;
            CargoDetailsDataSource.Select();           
            CargoDetailsView.DataBind();
        }
   
        protected void OnSaveInsert(object sender, EventArgs e)
        {
            CargoDetailsDataSource.InsertParameters["cargoName"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoName")).Text;
            CargoDetailsDataSource.InsertParameters["cargoDescription"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoDescription")).Text;
            CargoDetailsDataSource.InsertParameters["cargoShortCode"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoShortCode")).Text;
            CargoDetailsDataSource.InsertParameters["cargoCode"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoCode")).Text;
            CargoDetailsDataSource.InsertParameters["cargoETSNGName"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoNameETSNG")).Text;
            CargoDetailsDataSource.InsertParameters["cargoCodeGNG"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoCodeGNG")).Text;
            CargoDetailsDataSource.InsertParameters["cargoGNGName"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoNameGNG")).Text;
            CargoDetailsDataSource.InsertParameters["cargoMnemocode"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("InsertDetailsCargoMnemocode")).Text;
            CargoDetailsDataSource.InsertParameters["cargoIsUsed"].DefaultValue = ((CheckBox)CargoDetailsView.FindControl("InsertDetailsCargoIsUsed")).Checked.ToString();
            CargoDetailsDataSource.InsertParameters["cargoIsEmpty"].DefaultValue = ((CheckBox)CargoDetailsView.FindControl("InsertDetailsCargoIsEmpty")).Checked.ToString();

            CargoDetailsDataSource.Insert();
            CargoDetailsView.DataBind();
            GridViewForCargo.DataBind();

            CargoDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            CargoDetailsView.Visible = false;
            EditButtonID.Enabled = false;
            DeleteButtonID.Enabled = false;
        }

        protected void OnSaveEdit(object sender, EventArgs e)
        {
            CargoDetailsDataSource.UpdateParameters["cargoName"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoName")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoDescription"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoDescription")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoShortCode"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoShortCode")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoCode"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoCode")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoETSNGName"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoNameETSNG")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoCodeGNG"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoCodeGNG")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoGNGName"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoNameGNG")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoMnemocode"].DefaultValue = ((TextBox)CargoDetailsView.FindControl("EditDetailsCargoMnemocode")).Text;
            CargoDetailsDataSource.UpdateParameters["cargoIsUsed"].DefaultValue = ((CheckBox)CargoDetailsView.FindControl("EditDetailsCargoIsUsed")).Checked.ToString();
            CargoDetailsDataSource.UpdateParameters["cargoIsEmpty"].DefaultValue = ((CheckBox)CargoDetailsView.FindControl("EditDetailsCargoIsEmpty")).Checked.ToString();

            CargoDetailsDataSource.Update();
            CargoDetailsView.DataBind();
            GridViewForCargo.DataBind();

            CargoDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            CargoDetailsView.Visible = false;
            EditButtonID.Enabled = false;
            DeleteButtonID.Enabled = false;

        }
        
        protected void OnCancel(object sender, EventArgs e)
        {
            CargoDetailsView.ChangeMode(DetailsViewMode.ReadOnly);                    
        }

        protected void OnFirstPage(object sender, EventArgs e)
        {
            //CurrentCargoPage = 1;
            //PrevPageButton.Enabled = false;
            //NextPageButton.Enabled = true;
            //CargoDataSource.SelectParameters["pageIndex"].DefaultValue = CurrentCargoPage.ToString();
            //CargoDataSource.Select();
            //GridViewForCargo.DataBind();
        }

        protected void OnLastPage(object sender, EventArgs e)
        {
            //CurrentCargoPage = LastCargoPage;
            //PrevPageButton.Enabled = true;
            //NextPageButton.Enabled = false;
            //CargoDataSource.SelectParameters["pageIndex"].DefaultValue = CurrentCargoPage.ToString();
            //CargoDataSource.Select();
            //GridViewForCargo.DataBind();
        }

        protected void OnPrevPage(object sender, EventArgs e)
        {
            //if (CurrentCargoPage == 1)
            //{
            //    return;
            //}
            //CurrentCargoPage--;
            //if (CurrentCargoPage == 1)
            //{
            //    PrevPageButton.Enabled = false;
            //}

            //if (CurrentCargoPage == LastCargoPage-1)
            //{
            //    NextPageButton.Enabled = true;
            //}
            //CargoDataSource.SelectParameters["pageIndex"].DefaultValue = CurrentCargoPage.ToString();
            //CargoDataSource.Select();
            //GridViewForCargo.DataBind();
        }
        protected void OnNextPage(object sender, EventArgs e)
        {
            //if (CurrentCargoPage == LastCargoPage)
            //{
            //    return;
            //}
            //CurrentCargoPage++;
            //if (CurrentCargoPage == LastCargoPage)
            //{
            //    NextPageButton.Enabled = false;
            //}
            //if (CurrentCargoPage == 2)
            //{
            //    PrevPageButton.Enabled = true;
            //}

            //CargoDataSource.SelectParameters["pageIndex"].DefaultValue = CurrentCargoPage.ToString();
            //CargoDataSource.Select();
            //GridViewForCargo.DataBind();
        }        

        protected void CargoDetailsDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //CargoDetailsDataSource.SelectParameters["pageIndex"].DefaultValue = CurrentCargoPage.ToString();
        }
    }
}